#!/usr/bin/env python
# coding=utf-8
import os
from flask import Flask, render_template, request, make_response, redirect, url_for
from werkzeug.utils import secure_filename

app = Flask(__name__)


@app.route("/")
def index():
    return render_template("upload.html")


@app.route("/hello/")
@app.route("/hello/<name>")
def hello_word(name=None):
    return render_template("hello.html", name=name)


@app.route("/user/<username>")
def show_user_profile(username):
    return "Username:%s" % username


@app.route("/post/<int:post_id>")
def show_post(post_id):
    return "post_id=%d" % post_id


@app.route("/price/<float:price>")
def show_price(price):
    return "price=%f" % price


@app.route("/get_param/")
def show_get_param():
    name = request.args.get("name", "")
    return render_template("get_param.html", name=name)


@app.route("/form_param/", methods=['POST'])
def show_form_param():
    phone = request.form['phone']
    passwd = request.form['passwd']
    return render_template("form_param.html", phone=phone, passwd=passwd)


# @app.route("/upload/", methods=["POST"])
def upload_file():
    f = request.files['file']
    f.save(os.path.join(os.path.dirname(__file__), "upload/1.txt"))
    return "Finish"


@app.route("/upload/", methods=["POST"])
def upload_file():
    f = request.files['file']
    f.save(os.path.join(os.path.dirname(__file__), "upload", secure_filename(f.filename)))
    return "Finish"


@app.route("/login/<username>")
def add_cookies(username):
    res = make_response(render_template("hello.html"))
    res.set_cookie('username', username)
    return res


@app.route("/show_cookie/")
def show_cookies():
    username = request.cookies.get('username', '')
    return render_template("show_cookie.html", username=username)


@app.route("/show_redirect/")
def show_redirect():
    return redirect(url_for("show_cookies"))


if __name__ == '__main__':
    app.debug = True
    app.run()
